#include <stdio.h>
#include "Hashmap.h"
#include <string.h>

#include <stdlib.h>


#if 1
#define DBG(fmt,...) printf(fmt,##__VA_ARGS__)
#else
#define DBG(fmt,...) 
#endif



/*测试：打印哈希表*/
int hashmap_prt(sHASHMAP* map)
{
	if(NULL == map) return -1;
	int tab =90;
	int column = 15;
	DBG("\n%*s hashmap:\n", tab, "");
	DBG("%*s size: %d \n", tab, "", map->size);
	DBG("%*s listSize: %d \n", tab, "",map->listSize);
	DBG("%*s clash_num: %d \n", tab, "", map->clash_num);
	DBG("%*s autoAssign: %s\n", tab, "", (map->autoAssign) ? "true" : "false");

	tab += 3;
	if(map->size){
		DBG("%*s %s %*s %s %*s %s\n", tab, "","index", (int)(column-strlen("index")), "", "key", (int)(column-strlen("key")), "", "value");

	}else{
		DBG("%*s 空表 \n", tab, "");
	}
	
	sENTRY * entry=NULL;
	char* key = NULL;
	char* value = NULL;
	char index[32]={0};
	
    for (int i = 0, j=0,root=1; i < map->listSize; i++) {
        entry = &map->list[i];

        while (entry){
			if(entry->key){
				key = entry->key;
				value = entry->value;
			    sprintf(index, "%d" , i);
				if(root){
					DBG("%*s %s %*s \"%s\" %*s \"%s\" \n", tab, "", index,(int)(column-strlen(index)), "", key, (int)(column-strlen(key)-2), "", value);
					root = 0;
				}else{
					DBG("%*s %s %*s \"%s\" %*s \"%s\" \n", tab, "", "\\",column-1, "", key, (int)(column-strlen(key)-2), "", value);
				}
				j++;

			}
			entry = entry->next;
        }
		root=1;
		
    }


	DBG("\n");
	return 0;
}




int main(void) {

DBG("\n\n创建哈希表\n");
    sHASHMAP* map = hashMap_create();
	if(NULL == map){
		DBG("error init\n");
		return -1;
	}
	hashmap_prt(map);

DBG("添加数据\n");
    hashMap_insert(map, "123", "1234");
    hashMap_insert(map, "qw_key", "");
    hashMap_insert(map, "asddd", "qwqweehjk");
    hashMap_insert(map, "4545_key", "4545_value");
    hashMap_insert(map, "121313", "343435");
    hashMap_insert(map, "asdqweeq$%$*&", "*(^(#$@$@&%(");
    hashMap_insert(map, "qqwqw", "ss");
	
	hashmap_prt(map);



DBG("查，改，取，删数据\n");
	char value_buff[32]={0};
	int value_len=0;
	
	char * f_str = "4545_key";
	DBG("操作key：\"%s\"\n", f_str);
	value_len = hashMap_exists(map, f_str);
    DBG("\t exists : \"%s\" value_len: %d\n", value_len!=-1 ? "true" : "false", value_len);
	if(value_len == hashMap_get_value(map, f_str, value_buff, sizeof(value_buff))){
		DBG("\t value  : \"%s\"  value_len: %d\n", value_buff, value_len);

	}

    DBG("\t remove : %s\n", hashMap_remove(map, f_str) ? "true" : "false");
    DBG("\t remove again: %s\n", hashMap_remove(map, f_str) ? "true" : "false");
    DBG("\t exists : %s\n", hashMap_exists(map, f_str)!=-1 ? "true" : "false");
	hashMap_get_value(map, f_str, value_buff, sizeof(value_buff));
	DBG("\t value  : %s\n", value_buff);

	hashmap_prt(map);


	
DBG("扩大空间测试\n");
    hashMap_insert(map, "1213", "1234");//刚删了一个，现加一个，还是7个
    hashMap_insert(map, "qw_k1ey", "qw_value");//加完8个，自动扩容
	hashmap_prt(map);
    hashMap_insert(map, "asd1dd", "qwqweehjk");
    hashMap_insert(map, "45145_key", "4545_value");
    hashMap_insert(map, "1211313", "343435");
    hashMap_insert(map, "as1dqweeq$%$*&", "*(^(#$@$@&%(");
    hashMap_insert(map, "qq1wqw", "ss");
	
	hashmap_prt(map);


DBG("缩小空间测试\n");
	hashMap_remove(map, "qw_k1ey");
	hashMap_remove(map, "45145_key");
	hashMap_remove(map, "1211313");
	hashMap_remove(map, "asd1dd");
	hashMap_remove(map, "1213");//8
	hashmap_prt(map);
	hashMap_remove(map, "qw_key");//7
	hashmap_prt(map);
	hashMap_remove(map, "qqwqw");
	hashMap_remove(map, "121313");	
	hashMap_remove(map, "asddd");
	hashmap_prt(map);
	
DBG("清空测试\n");
	DBG("清理前 map %p\n", map);
    hashMap_clean(map);
	hashmap_prt(map);
	DBG("清理后 map %p\n\n", map);

DBG("删除哈希表测试\n");
DBG("再次添加数据\n");
    hashMap_insert(map, "123", "1234");
    hashMap_insert(map, "qw_key", "");
    hashMap_insert(map, "asddd", "qwqweehjk");
    hashMap_insert(map, "4545_key", "4545_value");
    hashMap_insert(map, "121313", "343435");
    hashMap_insert(map, "asdqweeq$%$*&", "*(^(#$@$@&%(");
    hashMap_insert(map, "qqwqw", "ss");
	
	hashmap_prt(map);

DBG("删除哈希表\n");
	hashMap_delete(&map);
	hashmap_prt(map);
	if(map){
		DBG("map %p\n", map);
	}else{
		DBG("哈希表删除成功\n");
	}
    return 0;
}